package test.my.com.myapplicationtest.horizontal_progress_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import test.my.com.myapplicationtest.R;

/**
 * Author : cooper
 * Time :  2018/1/24 下午7:37
 * Description :
 */

public class HorizontalProgressView extends View {

    private Context mContext;

    private int mWidth = 100;
    private int mHeight = 20;

    private int mBgColor;
    private int mProgressColor;
    private int mRoundValue;

    private Paint mProgressPaint;
    private Paint mBgPaint;

    private RectF mProgressRect = new RectF();
    private RectF mBgRect;

    private int mSpace = 0;
    private float mProgress;

    public void setPositionCallBack(PositionCallBack positionCallBack) {
        this.positionCallBack = positionCallBack;
    }

    private PositionCallBack positionCallBack;


    public HorizontalProgressView(Context context) {
        super(context);
    }

    public HorizontalProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressView, defStyleAttr, 0);
        mBgColor = ta.getColor(R.styleable.HorizontalProgressView_mBgColor, Color.GRAY);
        mProgressColor = ta.getColor(R.styleable.HorizontalProgressView_mProgressColor, Color.BLUE);
        mRoundValue = ta.getDimensionPixelOffset(R.styleable.HorizontalProgressView_mRoundValue, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                getResources().getDisplayMetrics()));
        ta.recycle();
        init();
    }

    private void init() {
        mProgressPaint = new Paint();
        mProgressPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStyle(Paint.Style.FILL);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);

        mBgPaint = new Paint();
        mBgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setColor(mBgColor);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setStrokeCap(Paint.Cap.ROUND);

    }

    public void setProgress(float progress) {
        mProgress = progress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mProgressRect = new RectF(mSpace, mSpace, (mWidth- mSpace)*mProgress/100 , mHeight - mSpace);
        canvas.drawRoundRect(mBgRect,mRoundValue,mRoundValue,mBgPaint);
        canvas.drawRoundRect(mProgressRect,mRoundValue,mRoundValue,mProgressPaint);
        positionCallBack.getPosition(mProgressRect.right,mProgressRect.bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
        mBgRect = new RectF(mSpace, mSpace, mWidth - mSpace, mHeight - mSpace);
        mProgressRect = new RectF(mSpace, mSpace, (mWidth- mSpace)*mProgress/100 , mHeight - mSpace);
    }

    private int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = mWidth;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        this.mWidth = result;
        return result;
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = mHeight;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        this.mHeight = result;
        return result;
    }

    public interface PositionCallBack {
        void getPosition(float x, float y);
    }


}
